<?php

namespace frontend\controllers;

use bc\controller\FrontController;
use bc\model\source\StatisticsModel;
use bc\model\source\LogModel;

class SourceStatisticsController extends FrontController
{
	
	public function actionIndex()
	{
		$id = $this->get('id');
		
		if(!$id)
			$this->redirect('/');
		
		$StatisticsModel = new StatisticsModel();
		$row = $StatisticsModel->getRow(['id' => $id, 'status' => '1']);
		
		if(!$row)
			$this->redirect('/');
		
		
		$session_id = $_COOKIE['zsmmssid']?$_COOKIE['zsmmssid']:session_id();
		setcookie('zsmmssid', $session_id, time()+3600*24*30);
		
		$logModel = new LogModel();
		$logRow = $logModel->getRow(['statistics_id' => $row['id'], 'session_id' => $session_id]);
		
		
		//写入日志表
		if(!$logRow)
		{
			$data['num'] = $row['num'] + 1;
			$StatisticsModel->modify($data, $row['id']);
			
			$log_data['session_id'] = $session_id;
			$log_data['statistics_id'] = $row['id'];
			$log_data['ip'] = $_SERVER['REMOTE_ADDR'];
			$log_data['http_user_agent'] = $_SERVER['HTTP_USER_AGENT'];
			$log_data['create_time'] = time();
			$logModel->add($log_data);
		}
		
		
		if($row['image'])
		{
			$this->redirect(STATIC_IMAGE . $row['image']);
		}
		else 
			$this->redirect($row['link']);
		
	}
	
}

